package com.cms.sync.service;

import java.util.ArrayList;
import java.util.List;

import com.cms.sync.model.KpayFeeData;
import com.cms.sync.model.KpaySyncLog;
import com.model.Admin;
import com.service.AbstractDateService;
import com.utils.dbcp.ModelGenerator;

public class KpaySyncLogServiceImpl extends AbstractDateService implements KpaySyncLogService {

	@Override
	public List<KpaySyncLog> getAll() {
		String sql = "select * from t_kpaysynclog";
		logger.info("[SQL] [method:getAll] ["+sql+"]");
		ModelGenerator<KpaySyncLog> model = new ModelGenerator<KpaySyncLog>(KpaySyncLog.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public boolean save(KpaySyncLog t) {
		return true;
		/*if(t.getEvent().equalsIgnoreCase("onpay")){
			int billsum=0;
			float billamount=0l;
			if(t.getResultcode().equals("100000")){
				billsum=1;
				billamount=t.getOrderamount();
			}
			 String sql = "insert into t_kpaysynclog_onpay(day,hour,minute,appversionnumber,cpid,channelid,goodsid,eventid,sessionid,imsi,imei,appid,chid,packages,versioncode,versionname,androidid,model,sdkint,sdk,manufacturer,brand,board,product,device,simsn,mobile,mac,nettype,operator,operatorname,event,action,orderid,ordercode,ordername,orderamount,orderbillamount,orderbillsum,orderdesc,provider,resultcode,resultmsg,activity,createdate,destorydate,lifetime,eventtime,remark,createtime) values (" +
					t.getDay()+","+
					t.getHour()+","+
					t.getMinute()+","+
					"'"+t.getAppversionnumber()+"',"+
					t.getCpid()+","+
					t.getChannelid()+","+
					t.getGoodsid()+","+
					"'" +t.getEventid()+"',"+
					"'" +t.getSessionid()+"',"+
					"'" +t.getImsi()+"',"+
					"'" +t.getImei()+"',"+
					"'" +t.getAppid()+"',"+
					"'" +t.getChid()+"',"+
					"'" +t.getPackages()+"',"+
					"'" +t.getVersioncode()+"',"+
					"'" +t.getVersionname()+"',"+
					"'" +t.getAndroidid()+"',"+
					"'" +t.getModel()+"',"+
					"'" +t.getSdkint()+"',"+
					"'" +t.getSdk()+"',"+
					"'" +t.getManufacturer()+"',"+
					"'" +t.getBrand()+"',"+
					"'" +t.getBoard()+"',"+
					"'" +t.getProduct()+"',"+
					"'" +t.getDevice()+"',"+
					"'" +t.getSimsn()+"',"+
					"'" +t.getMobile()+"',"+
					"'" +t.getMac()+"',"+
					"'" +t.getNettype()+"',"+
					"'" +t.getOperator()+"',"+
					"'" +t.getOperatorname()+"',"+
					"'" +t.getEvent()+"',"+
					"'" +t.getAction()+"',"+
					"'" +t.getOrderid()+"',"+
					"'" +t.getOrdercode()+"',"+
					"'" +t.getOrdername()+"',"+
					t.getOrderamount()+","+
					billamount+","+
					billsum+","+
					"'" +t.getOrderdesc()+"',"+
					"'" +t.getProvider()+"',"+
					"'" +t.getResultcode()+"',"+
					"'" +t.getResultmsg()+"',"+
					"'" +t.getActivity()+"',"+
					"'" +t.getCreatedate()+"',"+
					"'" +t.getDestorydate()+"',"+
					t.getLifetime()+","+
					"'" +t.getEventtime()+"',"+
					"'" +t.getRemark()+"',"+
					"'" +t.getCreatetime()+"'"+
					")";
			logger.info("[SQL] [method:save] [type:onpay] ["+sql+"]");
			ConnectionManager.getInstance().execute(base,sql);
		}
		
		String sql = "insert into t_kpaysynclog(day,hour,minute,appversionnumber,cpid,channelid,goodsid,eventid,sessionid,imsi,imei,appid,chid,packages,versioncode,versionname,androidid,model,sdkint,sdk,manufacturer,brand,board,product,device,simsn,mobile,mac,nettype,operator,operatorname,event,action,orderid,ordercode,ordername,orderamount,orderdesc,provider,resultcode,resultmsg,activity,createdate,destorydate,lifetime,eventtime,remark,createtime) values (" +
				t.getDay()+","+
				t.getHour()+","+
				t.getMinute()+","+
				"'"+t.getAppversionnumber()+"',"+
				t.getCpid()+","+
				t.getChannelid()+","+
				t.getGoodsid()+","+
				"'" +t.getEventid()+"',"+
				"'" +t.getSessionid()+"',"+
				"'" +t.getImsi()+"',"+
				"'" +t.getImei()+"',"+
				"'" +t.getAppid()+"',"+
				"'" +t.getChid()+"',"+
				"'" +t.getPackages()+"',"+
				"'" +t.getVersioncode()+"',"+
				"'" +t.getVersionname()+"',"+
				"'" +t.getAndroidid()+"',"+
				"'" +t.getModel()+"',"+
				"'" +t.getSdkint()+"',"+
				"'" +t.getSdk()+"',"+
				"'" +t.getManufacturer()+"',"+
				"'" +t.getBrand()+"',"+
				"'" +t.getBoard()+"',"+
				"'" +t.getProduct()+"',"+
				"'" +t.getDevice()+"',"+
				"'" +t.getSimsn()+"',"+
				"'" +t.getMobile()+"',"+
				"'" +t.getMac()+"',"+
				"'" +t.getNettype()+"',"+
				"'" +t.getOperator()+"',"+
				"'" +t.getOperatorname()+"',"+
				"'" +t.getEvent()+"',"+
				"'" +t.getAction()+"',"+
				"'" +t.getOrderid()+"',"+
				"'" +t.getOrdercode()+"',"+
				"'" +t.getOrdername()+"',"+
				t.getOrderamount()+","+
				"'" +t.getOrderdesc()+"',"+
				"'" +t.getProvider()+"',"+
				"'" +t.getResultcode()+"',"+
				"'" +t.getResultmsg()+"',"+
				"'" +t.getActivity()+"',"+
				"'" +t.getCreatedate()+"',"+
				"'" +t.getDestorydate()+"',"+
				t.getLifetime()+","+
				"'" +t.getEventtime()+"',"+
				"'" +t.getRemark()+"',"+
				"'" +t.getCreatetime()+"'"+
				")";
		//logger.info("[SQL] [method:save] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);*/
	}

	@Override
	public List<KpayFeeData> getKpayFeeData(String startday,String endday, KpayFeeData feedata,String dimension,Admin admin) {
		List<String> fl = new ArrayList<String>();
		String ch="";
		if(checkQueryDimension(dimension, "day")){
			ch+="t.day day,";
			fl.add("t.day");
		}
		if(checkQueryDimension(dimension, "hour")){
			ch+="t.hour hour,";
			fl.add("t.hour");
		}
		if(checkQueryDimension(dimension, "minute")){
			ch+="t.minute minute,";
			fl.add("t.minute");
		}
		if(checkQueryDimension(dimension, "channel")){
			ch+="t.chid chid,t.channelid channelid,";
			fl.add("t.chid,t.channelid");
		}
		if(checkQueryDimension(dimension, "feecode")){
			ch+="t.ordercode feecode,t.ordername feename,t.orderamount price,";
			fl.add("t.ordername,t.ordercode");
		}
		if(checkQueryDimension(dimension, "errcode")){
			ch+="t.resultcode errcode,t.resultmsg errname,";
			fl.add("t.resultcode,t.resultmsg");
		}
		if(checkQueryDimension(dimension, "app")){
			ch+="t.appversionnumber appversionnumber,t.appid appid,";
			fl.add("t.appversionnumber,t.appid");
		}
		if(checkQueryDimension(dimension, "operator")){
			ch+="t.operator operator,t.operatorname operatorname,";
			fl.add("t.operator,t.operatorname");
		}
		if(checkQueryDimension(dimension, "provider")){
			ch+="t.provider provider,";
			fl.add("t.provider");
		}
		//case t.resultcode when '100000' then 1 else 0 end                  case t.resultcode when '100000' then t.orderamount else 0 end
		String sql="select "+ch+"count(*) reqsum,sum(t.orderamount) reqfee,sum(case t.resultcode when '100000' then 1 else 0 end) as billsum,sum(case t.resultcode when '100000' then t.orderamount else 0 end) as billfee from t_kpaysynclog t ";
		String w = " where t.day>="+startday+" and t.day<="+endday;
		if(feedata.getChid()!=null&&!"".equals(feedata.getChid())){
			sql+=" left join t_channel ch on ch.id=t.channelid ";
			w+=" and (t.chid like '%"+feedata.getChid()+"%' or ch.name like '%"+feedata.getChid()+"%')";
		}
		if(feedata.getFeecode()!=null&&!"".equals(feedata.getFeecode())){
			w+=" and t.ordername like '%"+feedata.getFeecode()+"%'";
		}
		if(feedata.getErrcode()!=null&&!"".equals(feedata.getErrcode())){ 
			w+=" and t.resultmsg like '%"+feedata.getErrcode()+"%'";
		}
		if(feedata.getAppid()!=null&&!"".equals(feedata.getAppid())){
			sql+=" left join t_appversioninfo av on t.appversionnumber=av.appversionnumber ";
			w+=" and av.appname like '%"+feedata.getAppid()+"%'";
		}
		if(feedata.getOperator()!=null&&!"".equals(feedata.getOperator())){
			w+=" and t.operator='"+feedata.getOperator()+"'";
		}
		if(feedata.getProvider()!=null&&!"".equals(feedata.getProvider())){
			w+=" and t.provider='"+feedata.getProvider()+"'";
		}
		if(!"true".equals(admin.getIsAdmin())){
			w+=" and t.channelid in (select channelid from t_adminchannel where adminid="+admin.getId()+")";
		}
		sql+=w+" group by "+fl.toString().replace("[", "").replace("]", "").replace(" ", "");
		//sql+=" order by sum,fee";
		
		logger.info("[sql] [method:getKpayFeeData] ["+sql+"]");
		ModelGenerator<KpayFeeData> model = new ModelGenerator<KpayFeeData>(KpayFeeData.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public int getFee(int day, String appversionnumber, String chid,
			String operator) {
		String sql = "select sum(orderamount) fee from t_kpaysynclog where event='onPay' and resultcode='100000' and  appversionnumber='"+appversionnumber+"' and chid='"+chid+"' and operatorname='"+operator+"' and day="+day;
		logger.info("[sql] [method:getFee] ["+sql+"]");
		ModelGenerator<Integer> model = new ModelGenerator<Integer>(Integer.class);
		return model.generate(base,sql.toString());
	}

	
}
